layui.use(['table', 'LayerForm', 'LayerAjax', 'DynamicDetail'], function () {
    var table = layui.table;
    var exbase = layui.exbase;
    var $ = layui.$;

    var detailTable = table.render({
        elem: '#detail_table_id',
        height: 'full-60',
        id: 'detail_table_id',
        url: ctxPath + '/task/page',
        method: 'POST',
        contentType: 'application/json',
        dataType: 'json',
        page: {
            layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'],
            first: false, //不显示首页
            last: false //不显示尾页
        },
        request: {
            pageName: 'current', // 页码的参数名称，默认：page
            limitName: 'size' // 每页数据条数的参数名，默认：limit
        },
        cols: [[ //表头
            {checkbox: true}
            , {field: 'id', title: 'ID', width: 80, sort: true, hide: true}
            , {field: 'moduleName', title: '模块名'}
            , {field: 'taskCode', title: '任务编码'}
            , {field: 'taskName', title: '任务名称'}
            , {
                field: 'immediatelyExecute', title: '立即执行', templet: function (d) {
                    if (d.immediatelyExecute === 1) {
                        return '<span style="color: blue">是</span>'
                    } else {
                        return '<span style="color: pink">否</span>';
                    }
                }
            }
            , {field: 'taskNotifyUrl', title: '任务通知地址'}
            , {field: 'delayTime', title: '延时时间（秒）'}
            , {
                field: 'notifyStatus', title: '通知状态', templet: function (d) {
                    if (d.notifyStatus === 0) {
                        return '<span style="color: blue">初始</span>'
                    } else if (d.notifyStatus === 1) {
                        return '<span style="color: blue">成功</span>';
                    } else if (d.notifyStatus === 2) {
                        return '<span style="color: red">失败重试中</span>';
                    } else {
                        return '<span style="color: red">失败</span>';
                    }
                }
            }
            , {field: 'createTime', title: '创建时间'}
            , {field: 'right', title: '操作', width: 134, minWidth: 125, toolbar: '#operator'}
        ]],
        parseData: function (res) {
            return {
                "code": res.errorCode,
                "msg": res.errorMsg,
                "count": res.data.total,
                "data": res.data.records
            }
        },
    });

    layui.form.on('submit(search_button_filter)', function (data) {
        var moduleName = data.field.moduleName;
        if (!moduleName || moduleName === '') {
            moduleName = null;
        }
        var taskCode = data.field.taskCode;
        if (!taskCode || taskCode === '') {
            taskCode = null;
        }
        var taskName = data.field.taskName;
        if (!taskName || taskName === '') {
            taskName = null;
        }
        var notifyStatus = data.field.notifyStatus;
        if (!notifyStatus || notifyStatus === '') {
            notifyStatus = null;
        }
        detailTable.reload({
            page: {
                curr: 1,
            },
            where: {
                parameter: {
                    moduleName: moduleName,
                    taskCode: taskCode,
                    taskName: taskName,
                    notifyStatus: notifyStatus
                }
            }
        })
    });

    layui.form.on('submit(reset_button_filter)', function (data) {
        $("#keyword_input_id").val('');
        $("#task_code_input_id").val('');
        $("#task_name_input_id").val('');
        $('#notify_status_input_id').html('');
        detailTable.reload({
            page: {
                curr: 1,
            },
            where: {
                parameter: {
                    moduleName: null,
                    taskCode: null,
                    taskName: null,
                    notifyStatus: null
                }
            }
        })
    });

    table.on('tool(detail_table_id)', function (obj) {
        if (obj.event === 'detail') {
            detail(obj)
        }
        if (obj.event === 'retry') {
            retryEvent(obj)
        }
    });

    // 重试
    function retryEvent(obj) {
        var msg = '是否重试';
        layer.confirm(msg, function (index) {
            layer.close(index);
            let data = {
                "id": obj.data.id
            }
            // 重试消息
            $.ajax({
                async: false,
                url: ctxPath + '/task/retry',
                method: 'POST',
                contentType: 'application/json',
                dataType: 'json',
                data: JSON.stringify(data),
                success: function (data) {
                    if (data.errorCode == exbase.AJAX_SUCCESS_CODE) {
                        layer.msg("重试成功，等3秒后刷新页面", {icon: 1});
                    } else {
                        layer.msg(msg + "失败<br>" + data.errorMsg, {icon: 2});
                    }
                }
            })
        });
    }

    // 详情弹框
    function detail(obj) {
        var data = obj.data; // 得到当前行数据
        var detailId = data.id;
        var dialog = new layui.DynamicDetail({
            url: ctxPath + "/task/findDetail",
            method: 'POST',
            title: '延迟任务详情',
            contentType: 'application/json',
            data: JSON.stringify({"id": detailId}),
            area: ['420px', '450px'], // 宽高
            form: [{
                name: 'moduleName',
                label: '模块名称'
            }, {
                name: 'taskCode',
                label: '任务编码'
            },{
                name: 'taskName',
                label: '任务名称'
            },{
                name: 'immediatelyExecute',
                label: '立即执行',
            },{
                name: 'requestParam',
                label: '任务回调参数',
            },{
                name: 'requestMethod',
                label: '任务回调的请求方式',
            },{
                name: 'delayTime',
                label: '任务回调延时时间（秒）',
            },{
                name: 'notifyRetryNum',
                label: '通知地址重试次数',
            },{
                name: 'notifyNextTime',
                label: '下次通知的时间',
            },{
                name: 'notifyStatus',
                label: '通知状态',
            },{
                name: 'executeStatus',
                label: '执行状态',
            }],
            convert: function (name, value) {
                if (name === 'immediatelyExecute') {
                    if (value === 1) {
                        return '是';
                    }else {
                        return '否';
                    }
                }else if(name === 'notifyStatus') {
                    if (value === 1) {
                        return '成功';
                    }  else if(value === 2){
                        return '失败重试中';
                    } else if (value === 3) {
                        return '失败';
                    }else {
                        return '初始中';
                    }
                }else if (name === 'executeStatus') {
                    if (value === 1) {
                        return '运行中';
                    }else if (value === 2) {
                        return '运行中';
                    } else {
                        return '未执行';
                    }
                }else {
                    return value;
                }
            }
        })
        dialog.open();
    }
});